package com.baizhi.controller;

import com.baizhi.Animal;
import com.baizhi.service.AnimalService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AnimalController {
    @Autowired
    private AnimalService as;
    @GetMapping("animals")
    public Map<String,Object> selectByPage(int page,int limit){
        final PageInfo<Animal> pageInfo = as.selectByPage(page, limit);
        return new HashMap(){{
            put("code",0);
            put("count",pageInfo.getTotal());
            put("data",pageInfo.getList());
            put("msg","");
        }};
    }
    @DeleteMapping("animals/{id}")
    public void delete(@PathVariable("id")int id){
        as.delete(id);
    }
    @PostMapping("animals")
    public void insert(@RequestBody Animal animal){
        System.out.println(animal);
        as.insert(animal);
    }
    @DeleteMapping("animals")
    public void deleteAll(@RequestBody List<Integer> ids){
        System.out.println(ids);
        for (Integer id : ids) {
            as.delete(id);
        }
    }
    @GetMapping("animals/{id}")
    public Animal seletByAnimal(@PathVariable("id")int id){
        return as.seleteByID(id);
    }
    @PutMapping("animals/{id}")
    public void update(@RequestBody Animal a,@PathVariable int id){
        System.out.println(a);
        System.out.println(id);
        as.update(a,id);
    }
}
